﻿package com.wandian.view.mediator
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.InRoomPlayerDataProxy;
	import com.wandian.model.vo.PlayerVo;
	import com.wandian.model.vo.PropVo;
	import com.wandian.view.components.ui.Player;
	import com.wandian.view.components.ui.SoundManage;

	import flash.display.MovieClip;

	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;

	/**
	 * 玩家信息管理
	 * @author long
	 * @Create  2012-10-22
	 * @QQ 108232706
	 * @version 1.0
	 * 组件
	 * info1-9，玩家昵称与星级
	 * operate1-9 显示游戏金钱与操作状态[跟住，看牌]
	 * timeMc1-9 倒计时
	 * _mcFLManage 对某人施加动画，显示位置{fl1-9}
	 */
	public class PlayerMediator extends Mediator
	{
		/**
		 * 9个坐位的信息
		 * @default
		 */
		private var _arrPlayer:Array;
		private var _global:GameData;
		private var _inRoomData:InRoomPlayerDataProxy;
		private var _mcFLManage:MovieClip;
		public static const NAME:String="PlayerMediator";

		public function PlayerMediator(viewComponent:Object=null, mcFLManage:MovieClip=null)
		{
			this._arrPlayer=[];
			this._global=GameData.getInstance();
			this._mcFLManage=mcFLManage;
			super(NAME, viewComponent);
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			return;
		}

		private function init():void
		{
			//玩家昵称与星级
			var arrInfo:Array=[this.players.info1, this.players.info2, this.players.info3, this.players.info4, this.players.info5, this.players.info6, this.players.info7, this.players.info8, this.players.info9];
			//对玩家施放动画位
			var arrFM:Array=[this._mcFLManage.fl1, this._mcFLManage.fl2, this._mcFLManage.fl3, this._mcFLManage.fl4, this._mcFLManage.fl5, this._mcFLManage.fl6, this._mcFLManage.fl7, this._mcFLManage.fl8, this._mcFLManage.fl9];
			var arrTimer:Array=[this.players.timeMc1, this.players.timeMc2, this.players.timeMc3, this.players.timeMc4, this.players.timeMc5, this.players.timeMc6, this.players.timeMc7, this.players.timeMc8, this.players.timeMc9];
			//显示游戏金钱与操作状态[跟住，看牌]
			var arrOperate:Array=[this.players.operate1, this.players.operate2, this.players.operate3, this.players.operate4, this.players.operate5, this.players.operate6, this.players.operate7, this.players.operate8, this.players.operate9];
			//玩家赢了动画
			var arrWin:Array=[this.players.win1, this.players.win2, this.players.win3, this.players.win4, this.players.win5, this.players.win6, this.players.win7, this.players.win8, this.players.win9];
			var index:int=0;
			while (index < 9)
			{

				this._arrPlayer[index]=new Player((index + 1), arrInfo[index], arrFM[index], arrTimer[index], arrOperate[index], arrWin[index]);
				index=index + 1;
			}
			return;
		}

		/**
		 * 初始化9套玩家组建
		 */
		override public function onRegister():void
		{
			this.init();
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.SITDOWN_RSP, CLZNoticeName.STAND_UP_RSP, CLZNoticeName.OUT_GAME_ROOM_REMOVE, CLZNoticeName.UPDATE_GAME_PLAYER_STAKE, CLZNoticeName.SHOW, CLZNoticeName.OPERATION_PALYER, CLZNoticeName.OPERATION_SUCC, CLZNoticeName.GAME_OVER, CLZNoticeName.CUR_POT, CLZNoticeName.AGAIN_SIT_BUYIN_RES, CLZNoticeName.RESPONSE_ROOM_INFO_SUCC, CLZNoticeName.UPDATE_GAME_PLAYER_LASTACTION, CLZNoticeName.SHOW_FLASH_LEVEL_TO_SEAT_ID, CLZNoticeName.ONLINE_TIME_OUT_GAME, CLZNoticeName.UPDATE_SERVER_TIP];
		}

		override public function handleNotification(notification:INotification):void
		{
			var playerVo1:PlayerVo=null;
			var data:Object=notification.getBody();
			var playerVo:PlayerVo=null;
			var player:Player=null;
			var propVo:PropVo=null;
			var type:int=0;
			var playerVo2:PlayerVo=null;
			var arrSitPlayer:Array=null;
			var playerCount:int=0;
			var showOperateResult:String=null;
			var seatId:int=0;
			var index:int=0;
			var _loc_14:String=null;
			switch (notification.getName())
			{
				case CLZNoticeName.SITDOWN_RSP: //人物坐下
				{
					playerVo=notification.getBody() as PlayerVo;
					if (playerVo.userid == this._global.userInfo.userVo.userid)
					{
						this._global.seatId=playerVo.seatId;
//						this.sendNotification(CLZNoticeName.ONLINE_TIME_NUM, [this._global._onLineSitTime, this._global._onLineRewardNum]);
//						if (this._global.judgeMoneyType(GameData._objRoom.type))
//						{
//							this._global.userInfo.money=playerVo.money;
//						}
//						else
//						{
//							this._global.userInfo.money=playerVo.money;
//						}
						this._global.userInfo.money=playerVo.money;
					}
					if (playerVo.state != GameData.PLAYING_GAME)
					{
						if (playerVo.state != GameData.LOOK_ON)
						{
							if (playerVo.state == 0)
							{
								playerVo.lastAction=GameData.WAIT_NEXT_GAME;
							}
							else
							{
								playerVo.lastAction=playerVo.state;
							}
						}
					}				
					playerVo.level=GameData.getLevel(playerVo.money);
					//取人物坐位图片
//					player=this._arrPlayer[(playerVo.seatId - 1)];
//					propVo=GameData.getPropById(3, playerVo.personId);
//					playerVo.sex=propVo.type;
					playerVo.sex=1;
					this._inRoomData.sitDown(playerVo);
					player=this._arrPlayer[(data.seatId - 1)];
					player.sitDown(playerVo);
					player.updateLastAction(playerVo);
					type=int(notification.getType());
					if (!type)
					{
						SoundManage.playSound("SitDown", playerVo.sex);
					}
					break;
				}
				case CLZNoticeName.STAND_UP_RSP:
				{
					playerVo2=this._inRoomData.getSitPlayerBySeatId(data.seatId) as PlayerVo;
					if (playerVo2 == null)
						return;
					playerVo1=this._inRoomData.getInRoomPlayer(playerVo2.userid) as PlayerVo;
					if (playerVo1 == null)
						return;
					playerVo1.money=data.money;
					/*if (this._global.judgeMoneyType(GameData._objRoom.type))
					{
						playerVo1.money=data.money;
					}
					else
					{
						playerVo1.money=data.money;
					}*/
					if (playerVo1.userid == this._global.userInfo.userVo.userid)
					{
//						this._global.seatId=0;//玩家离开坐位，
						/*if (this._global.judgeMoneyType(GameData._objRoom.type))
						{
							this._global.userInfo.money=playerVo1.money;
						}
						else
						{
							this._global.userInfo.money=playerVo1.money;
						}*/
						this._global.userInfo.money=playerVo1.money;
						this._global.userInfo.level=GameData.getLevel(this._global.userInfo.userVo.money);
					}
					playerVo1.state=0;//GameData.LOOK_ON;
					if (playerVo1.seatId != 0)
					{
						this._arrPlayer[(playerVo1.seatId - 1)].standUp(playerVo1);
						this._inRoomData.standUp(data.seatId);
						this._arrPlayer[(playerVo1.seatId - 1)].init();
					}
					arrSitPlayer=[];
					index=0;
					while (index < this._inRoomData.sitAllPlayer.length)
					{

						playerVo1=this._inRoomData.sitAllPlayer[index] as PlayerVo;
						if (playerVo1 != null && playerVo1.userid > 0)
						{
							arrSitPlayer.push(playerVo1.seatId);
						}
						index=index + 1;
					}
					if (arrSitPlayer.length == 1) //若当前坐位玩家为1，游戏结束
					{
						this._arrPlayer[(arrSitPlayer[0] - 1)].init();
						trace("CLZNoticeName.GAME_OVER_CHECK");
						this.sendNotification(CLZNoticeName.GAME_OVER_CHECK, arrSitPlayer[0]);
					}
					SoundManage.playSound("StandUp", playerVo2.sex);
					break;
				}
				case CLZNoticeName.OUT_GAME_ROOM_REMOVE:
				{
					playerCount=this._arrPlayer.length;
					index=0;
					while (index < playerCount)
					{

						player=this._arrPlayer[index] as Player;
						player.removeThis();
						index=index + 1;
					}
					break;
				}
				case CLZNoticeName.UPDATE_GAME_PLAYER_STAKE:
				{
					playerVo1=notification.getBody() as PlayerVo;
					if (playerVo1.seatId == 0)
					{
						return;
					}
					player=this._arrPlayer[(playerVo1.seatId - 1)] as Player;
					player.updateStake(playerVo1);
					break;
				}
				case CLZNoticeName.UPDATE_GAME_PLAYER_LASTACTION:
				{
					playerVo1=notification.getBody() as PlayerVo;
					if (playerVo1.seatId == 0)
					{
						return;
					}
					player=this._arrPlayer[(playerVo1.seatId - 1)] as Player;
					player.updateLastAction(playerVo1);
					break;
				}
				case CLZNoticeName.OPERATION_SUCC:
				{
					if (data.seatId < 1 || data.seatId > 9)
						return;
					//找到成功的操作人,更新筹码，状态，隐藏倒计时，播放action声音，显示msg
					playerVo1=this._inRoomData.getSitPlayerBySeatId(data.seatId);
					playerVo1.stake=data.stake;
					playerVo1.callChip=data.callChip;
					playerVo1.lastAction=data.lastAction;
					player=this._arrPlayer[(data.seatId - 1)] as Player;
					player.updateStake(playerVo1);
					player.updateLastAction(playerVo1);
					player.hideTimer();
					switch (playerVo1.lastAction)
					{
						case 1: //看牌
						{
							showOperateResult=GameData.replaceStrValue(GameData.tip["MSG_PLAYER_CHECK"], "NAME", playerVo1.name);
							SoundManage.playSound("Check", playerVo1.sex, playerVo1.initSex);
							break;
						}
						case 2: //弃牌
						{
							showOperateResult=GameData.replaceStrValue(GameData.tip["MSG_PLAYER_FOLD"], "NAME", playerVo1.name);
							SoundManage.playSound("Fold", playerVo1.sex, playerVo1.initSex);
							break;
						}
						case 3: //跟注
						{
							showOperateResult=GameData.replaceStrValue(GameData.tip["MSG_PLAYER_CALL"], "NAME", playerVo1.name);
							showOperateResult=GameData.replaceStrValue(showOperateResult, "VALUE", playerVo1.callChip);
							SoundManage.playSound("Call", playerVo1.sex, playerVo1.initSex);
							break;
						}
						case 4: //加注
						{
							showOperateResult=GameData.replaceStrValue(GameData.tip["MSG_PLAYER_RAISE"], "NAME", playerVo1.name);
							showOperateResult=GameData.replaceStrValue(showOperateResult, "VALUE", playerVo1.callChip);
							SoundManage.playSound("Raise", playerVo1.sex, playerVo1.initSex);
							break;
						}
						case 5: //全押
						{
							showOperateResult=GameData.replaceStrValue(GameData.tip["MSG_PLAYER_ALLIN"], "NAME", playerVo1.name);
							SoundManage.playSound("AllIn", playerVo1.sex, playerVo1.initSex);
//							SoundManage.playSound("AllIn", 0, playerVo1.initSex);
//							SoundManage.playSound("AllIn", 1, playerVo1.initSex);
							break;
						}
						default:
						{
							break;
						}
					}

					this.sendNotification(CLZNoticeName.CHAT_SYSTEM_TIP, showOperateResult);
					break;
				}
				case CLZNoticeName.OPERATION_PALYER:
				{
					playerVo1=this._inRoomData.getSitPlayerBySeatId(data.seatId) as PlayerVo;
					if (playerVo1.userid != 0 && playerVo1.state == GameData.WAIT_NEXT_GAME)
					{
						return;
					}
//					if (playerVo1.state == GameData.WAIT_NEXT_GAME)
//					{
//						return;
//					}
					player=this._arrPlayer[(data.seatId - 1)] as Player;
					player.showTimer(playerVo1);
					break;
				}
				case CLZNoticeName.CUR_POT:
				{
					playerCount=this._arrPlayer.length;
					index=0;
					while (index < playerCount)
					{

						player=this._arrPlayer[index] as Player;
						playerVo1=this._inRoomData.getSitPlayerBySeatId((index + 1)) as PlayerVo;
						player.init(playerVo1);
						index=index + 1;
					}
					break;
				}
				case CLZNoticeName.SHOW:
				{
					playerCount=data.arrPlayer.length;
					index=0;
					while (index < playerCount)
					{

						playerVo1=this._inRoomData.getSitPlayerBySeatId(data.arrPlayer[index].seatId) as PlayerVo;
						playerVo1.lastAction=data.arrPlayer[index].cardType + 10;
						playerVo1.isWin=data.arrPlayer[index].isWin;
						if (playerVo1.isWin)
						{
							playerVo1.winGame++;
						}
						this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_LASTACTION, playerVo1);
						index=index + 1;
					}
					break;
				}
				case CLZNoticeName.GAME_OVER:
				{
					index=0;
					while (index < this._arrPlayer.length)
					{

						playerVo1=this._inRoomData.getSitPlayerBySeatId((index + 1)) as PlayerVo;
						playerVo1.isSend=false;
						trace("playerVo.userid:" + playerVo1.userid);
						if (playerVo1.userid == 0)
						{
							trace("playerVo");
						}
						else
						{
							playerVo1.state=GameData.WAIT_NEXT_GAME;
							if (playerVo1.lastAction != GameData.WAIT_BUY_IN)
							{
							}
							if (playerVo1.lastAction != GameData.PLAYING_CLOSE_SOCKE)
							{
								playerVo1.lastAction=GameData.WAIT_NEXT_GAME;
							}
							playerVo1.stake=playerVo1.stake + playerVo1.giveChip;
//							var _loc_15:*=playerVo1;
//							var _loc_16:*=playerVo1.sumGame + 1;
//							_loc_15.sumGame=_loc_16;
							playerVo1.sumGame++;
							playerVo1.giveChip=0;
							this._arrPlayer[index].init();
							this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_LASTACTION, playerVo1);
						}
						index=index + 1;
					}
					if (GameData.IS_UPDATE_SERVER)
					{
						data={okNoticeName: CLZNoticeName.RELOAD_WINDOW_PAGE, tip: GameData.tip["SERVER_UPDATE_TIP_1"], tipTitle: GameData.tip["SYSTEM_TIP"], isCancel: false};
						this.sendNotification(CLZNoticeName.SHOW_WINDOWS_TIP, data);
					}
					break;
				}
				case CLZNoticeName.AGAIN_SIT_BUYIN_RES:
				{
					if (data.errId > 0)
					{
						this.sendNotification(CLZNoticeName.STAND_UP);
						this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.tip["BUYIN_ERR"]});
						return;
					}
					playerVo1=this._inRoomData.getSitPlayerBySeatId(data.seatId) as PlayerVo;
					if (playerVo1.userid == this._global.userInfo.userVo.userid)
					{
						this._global.seatId=playerVo1.seatId;
						if (this._global.judgeMoneyType(GameData._objRoom.type))
						{
							this._global.userInfo.money=data.money;
						}
						else
						{
							this._global.userInfo.money=data.money;
						}
					}
					playerVo1.stake=data.stake;
					if (this._global.judgeMoneyType(GameData._objRoom.type))
					{
						playerVo1.money=data.money;
					}
					else
					{
						playerVo1.money=data.money;
					}
					playerVo1.lastAction=GameData.WAIT_NEXT_GAME;
					this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_STAKE, playerVo1);
					this.sendNotification(CLZNoticeName.UPDATE_GAME_PLAYER_LASTACTION, playerVo1);
					break;
				}
				case CLZNoticeName.RESPONSE_ROOM_INFO_SUCC:
				{
					if (data.curSeatId > 0 && data.curSeatId <= 9)
					{
						this._arrPlayer[(data.curSeatId - 1)].showTimer(); //当前坐位显示倒计时
					}
					break;
				}
				case CLZNoticeName.SHOW_FLASH_LEVEL_TO_SEAT_ID:
				{
					seatId=notification.getBody() as int;
					if(this._arrPlayer[seatId - 1]!=null)
					this._arrPlayer[(seatId - 1)].playLevel(5);
					break;
				}
				case CLZNoticeName.ONLINE_TIME_OUT_GAME:
				{
					playerVo1=this._inRoomData.getSitPlayerByMid(this._global.userInfo.userVo.userid);
					if (playerVo1.state != GameData.PLAYING_GAME)
					{
						this.sendNotification(CLZNoticeName.STAND_UP);
						_loc_14=GameData.tip["ONLINE_TIME_TIP_3"];
						data={okNoticeName: CLZNoticeName.GO_VERIFICATION, tip: _loc_14, tipTitle: GameData.tip["SYSTEM_TIP"], okTip: "GO_VERIFICATION", countTime: 60 * 60 * 5};
						this.sendNotification(CLZNoticeName.SHOW_WINDOWS_TIP, data);
					}
					break;
				}
				case CLZNoticeName.UPDATE_SERVER_TIP:
				{
					if (this._global.seatId == 0)
					{
						data={okNoticeName: CLZNoticeName.RELOAD_WINDOW_PAGE, tip: GameData.tip["SERVER_UPDATE_TIP_2"], tipTitle: GameData.tip["SYSTEM_TIP"], isCancel: false};
						this.sendNotification(CLZNoticeName.SHOW_WINDOWS_TIP, data);
						this.sendNotification(CLZNoticeName.CHAT_RES, {mid: 0, msg: GameData.tip["SERVER_UPDATE_TIP_2"], type: 2});
					}
					else
					{
						playerVo1=this._inRoomData.getSitPlayerBySeatId(this._global.seatId) as PlayerVo;
						if (playerVo1.state == GameData.PLAYING_GAME)
						{
							GameData.IS_UPDATE_SERVER=true;
							this.sendNotification(CLZNoticeName.CHAT_RES, {mid: 0, msg: GameData.tip["SERVER_UPDATE_TIP_1"], type: 2});
						}
						else
						{
							data={okNoticeName: CLZNoticeName.RELOAD_WINDOW_PAGE, tip: GameData.tip["SERVER_UPDATE_TIP_2"], tipTitle: GameData.tip["SYSTEM_TIP"], isCancel: false};
							this.sendNotification(CLZNoticeName.SHOW_WINDOWS_TIP, data);
							this.sendNotification(CLZNoticeName.CHAT_RES, {mid: 0, msg: GameData.tip["SERVER_UPDATE_TIP_2"], type: 2});
						}
					}
					break;
				}
				case CLZNoticeName.SHOW_CARD_TIP:
				{
					player=this._arrPlayer[(data.seatId - 1)] as Player;
					if (player != null)
					{
						player.hideNameInfo();
					}
					break;
				}
				case CLZNoticeName.HIDE_CARD_TIP:
				{
					player=this._arrPlayer[(data.seatId - 1)] as Player;
					playerVo=this._inRoomData.getSitPlayerBySeatId(data.seatId) as PlayerVo;
					if (playerVo.userid != 0)
					{
						player.showNameInfo();
					}
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function get players():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
